#include <base\plane.h>

plane::plane(vec3f &v1, vec3f &v2, vec3f &v3) {
	set3Points(v1,v2,v3);
}


plane::plane() {
}

plane::~plane() {
}

void 
Plane::set3Points(vec3f &v1, vec3f &v2, vec3f &v3) {

	vec3f aux1, aux2;

	aux1 = v1 - v2;
	aux2 = v3 - v2;

	normal = aux2.cross(aux1);

	normal.normalize();
	point = v2;
	d = -(normal.dot(point));
}

void 
plane::setNormalAndPoint(vec3f &normal_, vec3f &point) {

	normal = normal_;
	normal.normalize();
	d = -(this->normal.dot(point));
}

void 
plane::setCoefficients(float a, float b, float c, float d) {

	// set the normal vector
	normal.set(a,b,c);
	//compute the lenght of the vector
	float l = normal.length();
	// normalize the vector
	normal.set(a/l,b/l,c/l);
	// and divide d by th length as well
	this->d = d/l;
}

float 
plane::distance(vec3f &p) {
	return (d + normal.dot(p));
}
